﻿using UnityEngine;
using UnityEngine.UI;

public class GameMain : MonoBehaviour
{
    //private bool isDone = false;
    //private float time = 0;
    //private CharacterController controll;
    public GameObject gae;
    public RectTransform rect;
    public bool isEditor = false;

    internal static float lastGCTime = 0;
    internal const float GCInterval = 1;//1 second 

    private void Awake()
    {
        DontDestroyOnLoad(gameObject);
        LuaManager.Instance.Startup();
        UIManager.Instance.Startup();
        ResourceManager.Instance.Startup();
        SceneMgr.Instance.Startup();
        
        if (isEditor)
        {
            LuaManager.Instance.Require("mainEditor");
        }
        else
        {
            LuaManager.Instance.Require("main");
        }
    }

    private void Start()
    {
        //var worldPoint=  RectTransformUtility.WorldToScreenPoint(Camera.main, gae.transform.position);
        //Vector2 outPos = new Vector2(0, 0);
        //RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, worldPoint, UIManager.Instance.UICamera, out outPos);
        //print(outPos);
    }

    private void OnEnable()
    {
        //Debug.Log("OnEnabale");
    }

    private void OnDisable()
    {
        //Debug.Log("OnDisable");
    }

    //private IEnumerator LunchGame()
    //{
    //    yield return ResourceManager.Instance.LoadManifest();
    //}
    private void Demo()
    {
        //transform./*eulerAngles*/
        //transform.localEulerAngles.
        //Vector3.Angle
        //transform.SetPositionAndRotation(new Vector3(0,0,0),new Quaternion());
        //var mat = new Material();
        //var render = gameObject.GetComponent<Renderer>();
        //render.sharedMaterials
        //Toggle toggle = gameObject.GetComponent<Toggle>();
        //toggle.isOn = false;
        //toggle.onValueChanged.AddListener((bool v) => { });
        //Image image;
        //var render = gameObject.GetComponent<Renderer>();
        //render.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
        //transform.RotateAround(new Vector3(0, 0, 0), Vector3.up, 10);
        //RectTransform rectTransform;
        //Image image;
        //image.sprite =;
        //Outline outline;
        //Renderer renderer = gameObject.GetComponent<Renderer>();
        //renderer.material.SetTexture("_MainTex", self.);

        //renderer.materials = materils;
        //Material material = gameObject.GetComponentInChildren<Renderer>().material;
        //material.SetFloat("")
        //material.shader = Resources.Load<Shader>("");

        //image.sprite
        //Resources.Load<SpriteAtlas>();
        //Resources.Load<SpriteAltas>();
        //Image;
        //transform.SetParent()
        //var sequence = DOTween.Sequence();
        //var move = transform.Ro
        //move.SetLoops(-1);
        //move.onComplete=
        //Animator animator;
        //var controller = animator.runtimeAnimatorController;
        //AnimationClip clip = controller.animationClips[0];
        //var animationEventssss = clip.events;
        //animationEventssss[0].


        //transform.rotation
        //transform.LookAt(new Vector3(0, 0, 0));
        //Vector3 vec = new Vector3();
        //vec.Normalize();
        //GameObject.Find("name");
        //transform.Rotate(Vector3.up, time);
        //time += Time.deltaTime * 10;
        //controll.SimpleMove();
    }

    private void Update()
    {
        if (Time.time - GameMain.lastGCTime > GCInterval)
        {
            LuaManager.Instance.GetLuaEnv().Tick();
            GameMain.lastGCTime = Time.time;
        }
    }

    void OnUpdateFinish(int status)
    {
        print("log go to login");
        UIManager.Instance.PopWindow();
        UIManager.Instance.OpenWindows("LoginView");
    }

    void OnUpdateProgress(ulong curProgress,ulong totalProgress)
    {
        print(curProgress + "/" + totalProgress);
    }


    //void clickBegin(BaseEventData eventData)
    //{
    //    print("clickBegin(BaseEventData eventData)");
    //    print(((PointerEventData)eventData).position);
    //}
    //void clickDrag(BaseEventData eventData)
    //{
    //    print("clickDrag(BaseEventData eventData)");
    //    print(((PointerEventData)eventData).position);
    //}

    //void clickEnd(BaseEventData eventData)
    //{
    //    print("clickEnd(BaseEventData eventData)");
    //    //eventData.
    //}


}
